//矢量图层
var baseVectorLayer = null;
//矢量图层交互
var baseVectorInteraction = null;
//选中feature交互
var selectSingleClick = null;
//气泡layer
var overlay = null;

$(function(){
    //初始化地图
    mapUtil.initializeMap();
    //默认显示图层列表
    $('#layeList').show();
    $('#zhonguoqingzaiqingkuangxl').show();


    //矢量图公用Layer
    baseVectorLayer = new ol.layer.Vector({
        source: new ol.source.Vector(),
    });
    map.addLayer(baseVectorLayer);

    //选择矢量图显示信息
    selectSingleClick = new ol.interaction.Select();
    map.addInteraction(selectSingleClick);

    //选中矢量Feature显示气泡详情
    selectSingleClick.on('select', function(evt){
        var feature = evt.selected[0];
        if (feature) {
            var coordinate = evt.mapBrowserEvent.coordinate;
            var contentHtml = createOverlayContent(feature);//创建气泡里显示信息内容
            showOverlayer(coordinate, contentHtml);
        }
    });

    //添加overlay图层
    var container = document.getElementById('popup');
    overlay = new ol.Overlay({
        element: container,
        autoPan: true,
        autoPanAnimation: {
            duration: 250
        }
    });
    map.addOverlay(overlay);

    //模态窗口关闭图标
    $('.modal-header button').click(function() {
        destroyModal();
    });
    getTableAjax();

    removeLayerCheckbox();//默认取消勾选其他图层

    $('#list-btn').click();//默认 隐藏tab页

    //定位
    $("#viewIn").click(function(){
        var url = htport + '/mapset/getInfo.html';
        var position = [];
        $.get(url,function(data){
            for(var i =0, data = data.rows; i < data.length; i++){
                if (data[i].ifcheck == 1) {
                    position.push(data[i].lon,data[i].lat);
                }
            }
            map.getView().setCenter(position);
            map.getView().setZoom(10);
        });
    });
});

//调整主视图大小
adjustWindow();


//======================获取样地信息Begin====================//
//获取样地图层
function getTableAjax(){
    //清空地图上的矢量图
    if (baseVectorLayer) {
        baseVectorLayer.getSource().clear();
    }
/*    $.ajax({
        type: 'get',
        datatype:'json',
        url: htport + '/manage/getSoilList.html',
        success: function(data, textStatus, xhr){
            if (data) {

                for (var i = 0; i < data.length; i++) {
                    //绘制Feature
                    drawFeature(data[i].id, data[i].soilName, data[i].station,
                        data[i].listenPoint, data[i].description, data[i].type, data[i].coordinates);
                }
            }
        },
        error: function(error){
            alert('获取样地信息！');
        }
    });*/
}

//根据ajax请求动态在地图上添加Feature
function drawFeature(id, soilName, station, listenPoint, description, type, coordinates){
    var feature = null;
    if (type == 'Point') {
        feature = new ol.Feature({
            geometry: new ol.geom.Point(coordinates),
            id: id,
            soilName: soilName,
            station: station,
            listenPoint: listenPoint,
            description: description,
            type: type,
            coordinates: coordinates
        });
    } else if (type == 'Line') {
        feature = new ol.Feature({
            geometry: new ol.geom.LineString(coordinates),
            id: id,
            soilName: soilName,
            station: station,
            listenPoint: listenPoint,
            description: description,
            type: type,
            coordinates: coordinates
        });
    } else if (type == 'Polygon') {
        feature = new ol.Feature({
            geometry: new ol.geom.Polygon(coordinates),
            id: id,
            soilName: soilName,
            station: station,
            listenPoint: listenPoint,
            description: description,
            type: type,
            coordinates: coordinates
        });
    }

    //var url = htport + '/image/getImageSetInfo.html';
    var url="";
    $.get(url, function(data){
        var style = new ol.style.Style({
            stroke: new ol.style.Stroke({
                color: data.color,
                width: Number(data.geoSize.substring(0,data.geoSize.length-2))
            }),
            image: new ol.style.Circle({
                radius: Number(data.geoSize.substring(0,data.geoSize.length-2)),
                fill: new ol.style.Fill({
                    color: data.color
                })
            })
        });
        baseVectorLayer.setStyle(style);
    });


    baseVectorLayer.getSource().addFeature(feature);
}
//======================获取样地信息Begin====================//
//显示气泡
function showOverlayer(coordinate, contentHtml){
    $('#deviceList').html(contentHtml);
    overlay.setPosition(coordinate);
    map.getView().setZoom(9);
    map.getView().setCenter(coordinate);
}

//销毁气泡
function deviceList(){
    if (overlay) {
        overlay.setPosition(undefined);
    }
}

//创建气泡里显示信息内容
function createOverlayContent(feature) {
    var html = '<tr>'
        + '<td>' +feature.get('soilName') + '</td>'
        + '<td>' +feature.get('station') + '</td>'
        + '<td>' +feature.get('listenPoint') +'</td>'
        + '<td>' +feature.get('listenPoint') +'</td>'
        + '</tr>';
    return html;
}


/**
 * [点击叶窗触发事件]
 * @AuthorHTL zoro
 * @DateTime  2017-08-06T16:33:58+0800
 * @param     {[number]}
 */
function tabChanged(tabindex) {
    switch (tabindex) {
        case 1:
            $("#zhonguoqingzaiqingkuang").panelToggle('flexible-panel');
            break;
        case 3:
            $("#zhonguoqingzaiqingkuangxl").panelToggle('flexible-panel');
            break;
    }
    resizeBtnMiddle();
}

//默认 不勾选其他图层
function removeLayerCheckbox(){
    $('#dxtChk').click();
    $('#wptChk').click();
    $('#sxtChk').click();
    $('#zwtChk').click();
    $('#dwtChk').click();
    $('#gnqhtChk').click();
    $('#gnqwtChk').click();
    $('#rhyxChk').click();
    $('#yxtChk').click();
}

/**
 * type
 * data: "97.50708007812501,27.956542968750007" ==> [97.50708007812501,27.956542968750007]
 */
function formatLonLatArray(data){
    var tempArr = [];
    tempArr = data.split(",");
    var resArr = [];
    for (var i = 0; i< tempArr.length; i++) {
        resArr.push(Number(tempArr[i]));
    }
    return resArr;

}

var zoro = {
    chkIsDraw: function(flag) {
        if (flag) {
            $('#isDrawed').val('');
        }
    }
};

